package org.jeecg.modules.demo.peihebumenbiao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.demo.Shenhefankuibiao.entity.Shenhefankuibiao;
import org.jeecg.modules.demo.Shenhefankuibiao.mapper.ShenhefankuibiaoMapper;
import org.jeecg.modules.demo.Shenhefankuibiao.service.IShenhefankuibiaoService;
import org.jeecg.modules.demo.peihebumenbiao.entity.Peihebumenbiao;
import org.jeecg.modules.demo.peihebumenbiao.mapper.PeihebumenbiaoMapper;
import org.jeecg.modules.demo.peihebumenbiao.service.IPeihebumenbiaoService;
import org.jeecg.modules.demo.zhidingzhouqigongzuo.entity.Zhidingzhouqigongzuo;
import org.jeecg.modules.demo.zhidingzhouqigongzuo.mapper.ZhidingzhouqigongzuoMapper;
import org.jeecg.modules.demo.zhouqibiao.entity.Zhouqibiao;
import org.jeecg.modules.demo.zhouqibiao.mapper.ZhouqibiaoMapper;
import org.jeecg.modules.system.entity.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.concurrent.Callable;

/**
 * @Description: 配合部门任务表
 * @Author: jeecg-boot
 * @Date:   2025-10-09
 * @Version: V1.0
 */
@Service
public class PeihebumenbiaoServiceImpl extends ServiceImpl<PeihebumenbiaoMapper, Peihebumenbiao> implements IPeihebumenbiaoService {
    @Autowired
    private PeihebumenbiaoMapper peihebumenbiaoMapper;
    @Autowired
    private ZhouqibiaoMapper zhouqibiaoMapper;
    @Autowired
    private ZhidingzhouqigongzuoMapper zhidingzhouqigongzuoMapper;
    @Autowired
    private ShenhefankuibiaoMapper shenhefankuibiaoMapper;
    @Autowired
    private IShenhefankuibiaoService shenhefankuibiaoService;

    public String departId(){
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        String departId = sysUser.getDepartIds();
        return departId;
    }
    @Override
    public List<SysUser> getUserList() {
        String departId = departId();
        return peihebumenbiaoMapper.getPeihezhuguan(departId);
    }

    @Override
    public void initData(){
        //检查是否有初始数据
        //没有初始数据，则生成初始数据
        QueryWrapper<Zhidingzhouqigongzuo> qw = new QueryWrapper<>();
        qw.eq("shifoushengxiao","shi");
        List<Zhidingzhouqigongzuo> list = zhidingzhouqigongzuoMapper.selectList(qw);
        //没有初始化的数据都存入这个list中
        List<Zhidingzhouqigongzuo> wuchushidate = new ArrayList<>();
        if(list.size()>0){
            for(int i=0;i<list.size();i++){
                String id = list.get(i).getId();
                QueryWrapper<Zhouqibiao> qw2 = new QueryWrapper<Zhouqibiao>();
                qw2.eq("zhidingbiaoid",id);
                List<Zhouqibiao> list2 = zhouqibiaoMapper.selectList(qw2);
                if(list2.size()==0){
                    wuchushidate.add(list.get(i));
                    System.out.println("已生成初始数据");
                }else{
                    System.out.println("已存在初始数据");
                }
            }
        }else{
            System.out.println("暂无数据");
        }
        if(wuchushidate.size()>0){
            for(int i=0;i<wuchushidate.size();i++){
                Zhouqibiao zhouqibiao = new Zhouqibiao();
                zhouqibiao.setZhidingbiaoid(wuchushidate.get(i).getId());
                zhouqibiao.setWanchengshijian(wuchushidate.get(i).getXiacihuibaoshijian());
                zhouqibiao.setWanchengqingkuang("未开始");
                zhouqibiaoMapper.insert(zhouqibiao);
            }
        }

    }

    @Override
    public void TimedDataGenerator(){

        /**
        * 拿到生效的周期性任务
         * 假如：2025-10-09 00:00:00是上一次完成截至时间，周期单位为周，周期数量为2
         * 下一次完成截止时间：2025-10-23号 00:00:00
         * 客户的要求生成时间：在上一次完成截止时间的第二天凌晨3点，需要生成下一次的周期性任务
        * */
        //先获取某项目最近的一条记录，将各个项目最新的记录存入一个list中，用于生成下一条记录
        QueryWrapper<Zhidingzhouqigongzuo> qw = new QueryWrapper<>();
        qw.eq("shifoushengxiao","shi");
        List<Zhidingzhouqigongzuo> list = zhidingzhouqigongzuoMapper.selectList(qw);
        //定义一个存符合要求的周期指标
        List<Zhouqibiao> list2 = new ArrayList<>();
        for(int i=0;i<list.size();i++){
            String id = list.get(i).getId();
            QueryWrapper<Zhouqibiao> qw2 = new QueryWrapper<>();
            qw2.eq("zhidingbiaoid",id);
            qw2.orderByDesc("wanchengshijian");
            qw2.last("limit 1");
            Zhouqibiao zhouqibiao = zhouqibiaoMapper.selectOne(qw2);
            if(zhouqibiao != null && !zhouqibiao.getWanchengqingkuang().equals("未开始")){
                list2.add(zhouqibiao);
            }else{
                System.out.println("暂无数据");
                return;
            }
        }
        if(list2.size()>0){
            for(int i=0;i<list2.size();i++){
                Zhouqibiao zhouqibiao = new Zhouqibiao();
                zhouqibiao.setZhidingbiaoid(list2.get(i).getZhidingbiaoid());
                QueryWrapper<Zhidingzhouqigongzuo> qw3 = new QueryWrapper<>();
                qw3.eq("id",list2.get(i).getZhidingbiaoid());
                Zhidingzhouqigongzuo zhidingzhouqigongzuo = zhidingzhouqigongzuoMapper.selectOne(qw3);
                String zhouqi = zhidingzhouqigongzuo.getZhouqi();
                int shuliang = zhidingzhouqigongzuo.getShuliang();
                LocalDateTime now = LocalDateTime.now();
                if(zhouqi.equals("tian")){
                    //获取上次完成时间+周期 X 数量
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(list2.get(i).getWanchengshijian());
                    calendar.add(Calendar.DAY_OF_MONTH, shuliang);
                    Date date = Date.from(calendar.getTime().toInstant());
                    zhouqibiao.setWanchengshijian(date);
                    zhouqibiao.setWanchengqingkuang("未开始");
                } else if (zhouqi.equals("zhou")) {
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(list2.get(i).getWanchengshijian());
                    calendar.add(Calendar.WEEK_OF_MONTH, shuliang);
                    Date date = Date.from(calendar.getTime().toInstant());
                    zhouqibiao.setWanchengshijian(date);
                    zhouqibiao.setWanchengqingkuang("未开始");
                } else if (zhouqi.equals("yue")) {
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(list2.get(i).getWanchengshijian());
                    calendar.add(Calendar.MONTH, shuliang);
                    Date date = Date.from(calendar.getTime().toInstant());
                    zhouqibiao.setWanchengshijian(date);
                    zhouqibiao.setWanchengqingkuang("未开始");
                } else if (zhouqi.equals("nian")) {
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(list2.get(i).getWanchengshijian());
                    calendar.add(Calendar.YEAR, shuliang);
                    Date date = Date.from(calendar.getTime().toInstant());
                    zhouqibiao.setWanchengshijian(date);
                    zhouqibiao.setWanchengqingkuang("未开始");
                }else{
                    System.out.println("暂无该周期");
                    return;
                }
                zhouqibiaoMapper.insert(zhouqibiao);
            }
        }else{
            System.out.println("暂无数据需要生成周期性任务");
            return;
        }
       //拿到最近的一条数据之后，拷贝制定表id，计算下次完成时间，插入初始状态
    }

    //周期性生成反馈表中各个部门的数据
    @Override
    public void GenerateFeedbackTable(){
        QueryWrapper<Zhouqibiao> qw = new QueryWrapper<>();
        qw.eq("wanchengqingkuang","未开始");
        List<Zhouqibiao> list = zhouqibiaoMapper.selectList(qw);
        List<Shenhefankuibiao> list3 = new ArrayList<>();
        if(list.size()>0){
            for(int i=0;i<list.size();i++){
                String zhidingid = list.get(i).getZhidingbiaoid();
                QueryWrapper<Peihebumenbiao> qw2 = new QueryWrapper<>();
                qw2.eq("zhidingbiaoid",zhidingid);
                List<Peihebumenbiao> list2 = peihebumenbiaoMapper.selectList(qw2);

                QueryWrapper<Zhidingzhouqigongzuo> qw3 = new QueryWrapper<>();
                qw3.eq("id",zhidingid);
                Zhidingzhouqigongzuo zhidingzhouqigongzuo = zhidingzhouqigongzuoMapper.selectOne(qw3);
                for(int j=0;j<list2.size();j++){
                    Shenhefankuibiao shenhefankuibiao = new Shenhefankuibiao();
                    shenhefankuibiao.setZhidingbiaoid(zhidingid);
                    shenhefankuibiao.setZerenzhuguanid(zhidingzhouqigongzuo.getZerenzhuguanid());
                    shenhefankuibiao.setPeihebumenid(list2.get(j).getPeihebumenid());
                    shenhefankuibiao.setZhuangtai("未开始");
                    shenhefankuibiao.setWanchengshijian(list.get(i).getWanchengshijian());
                    list3.add(shenhefankuibiao);
                }
            }
            shenhefankuibiaoService.saveBatch(list3);
        }else{
            System.out.println("暂无数据");
            return;
        }

    }

    //更改周期性任务的状态，改变为进行中
    @Override
    public void updateZhouqiZhuangtai(){
        QueryWrapper<Zhouqibiao> qw1 = new QueryWrapper<>();
        qw1.eq("wanchengqingkuang","未开始");
        List<Zhouqibiao> list1 = zhouqibiaoMapper.selectList(qw1);
        QueryWrapper<Shenhefankuibiao> qw2 = new QueryWrapper<>();
        qw2.eq("zhuangtai","未开始");
        List<Shenhefankuibiao> list2 = shenhefankuibiaoMapper.selectList(qw2);
        //获取当前时间
        Date now = new Date();
        if(list1.size()>0){
            for(int i=0;i<list1.size();i++){
                QueryWrapper<Zhidingzhouqigongzuo> qw3 = new QueryWrapper<>();
                qw3.eq("id",list1.get(i).getZhidingbiaoid());
                Zhidingzhouqigongzuo zhidingzhouqigongzuo = zhidingzhouqigongzuoMapper.selectOne(qw3);
                String zhouqi = zhidingzhouqigongzuo.getZhouqi();
                int shuliang = zhidingzhouqigongzuo.getShuliang();
                Date wanchengshijian = list1.get(i).getWanchengshijian();
                //通过完成时间反推任务开始时间
                if(zhouqi.equals("tian")){
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(wanchengshijian);
                    calendar.add(Calendar.DAY_OF_MONTH, -shuliang);
                    Date renwukaishishijian = Date.from(calendar.getTime().toInstant());
                    if(now.after(renwukaishishijian) && now.before(wanchengshijian)){
                        list1.get(i).setWanchengqingkuang("进行中");
                        zhouqibiaoMapper.updateById(list1.get(i));
                    }
                } else if (zhouqi.equals("zhou")) {
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(wanchengshijian);
                    calendar.add(Calendar.WEEK_OF_MONTH, -shuliang);
                    Date renwukaishishijian = Date.from(calendar.getTime().toInstant());
                    if(now.after(renwukaishishijian) && now.before(wanchengshijian)){
                        list1.get(i).setWanchengqingkuang("进行中");
                        zhouqibiaoMapper.updateById(list1.get(i));
                    }
                } else if (zhouqi.equals("yue")) {
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(wanchengshijian);
                    calendar.add(Calendar.MONTH, -shuliang);
                    Date renwukaishishijian = Date.from(calendar.getTime().toInstant());
                    if(now.after(renwukaishishijian) && now.before(wanchengshijian)){
                        list1.get(i).setWanchengqingkuang("进行中");
                        zhouqibiaoMapper.updateById(list1.get(i));
                    }
                } else if (zhouqi.equals("nian")) {
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(wanchengshijian);
                    calendar.add(Calendar.YEAR, -shuliang);
                    Date renwukaishishijian = Date.from(calendar.getTime().toInstant());
                    if(now.after(renwukaishishijian) && now.before(wanchengshijian)){
                        list1.get(i).setWanchengqingkuang("进行中");
                        zhouqibiaoMapper.updateById(list1.get(i));
                    }
                }else{
                    System.out.println("暂无数据");
                    return;
                }
            }
        }
        if(list2.size()>0){
            for(int i=0;i<list2.size();i++){
                QueryWrapper<Zhidingzhouqigongzuo> qw3 = new QueryWrapper<>();
                qw3.eq("id",list2.get(i).getZhidingbiaoid());
                Zhidingzhouqigongzuo zhidingzhouqigongzuo = zhidingzhouqigongzuoMapper.selectOne(qw3);
                String zhouqi = zhidingzhouqigongzuo.getZhouqi();
                int shuliang = zhidingzhouqigongzuo.getShuliang();
                Date wanchengshijian = list2.get(i).getWanchengshijian();
                if(zhouqi.equals("tian")){
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(wanchengshijian);
                    calendar.add(Calendar.DAY_OF_MONTH, -shuliang);
                    Date renwukaishishijian = Date.from(calendar.getTime().toInstant());
                    if(now.after(renwukaishishijian) && now.before(wanchengshijian)){
                        list2.get(i).setZhuangtai("进行中");
                        shenhefankuibiaoMapper.updateById(list2.get(i));
                    }
                }else if (zhouqi.equals("zhou")) {
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(wanchengshijian);
                    calendar.add(Calendar.WEEK_OF_MONTH, -shuliang);
                    Date renwukaishishijian = Date.from(calendar.getTime().toInstant());
                    if(now.after(renwukaishishijian) && now.before(wanchengshijian)){
                        list2.get(i).setZhuangtai("进行中");
                        shenhefankuibiaoMapper.updateById(list2.get(i));
                    }
                }else if(zhouqi.equals("yue")){
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(wanchengshijian);
                    calendar.add(Calendar.MONTH, -shuliang);
                    Date renwukaishishijian = Date.from(calendar.getTime().toInstant());
                    if(now.after(renwukaishishijian) && now.before(wanchengshijian)){
                        list2.get(i).setZhuangtai("进行中");
                        shenhefankuibiaoMapper.updateById(list2.get(i));
                    }
                }else if(zhouqi.equals("nian")){
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(wanchengshijian);
                    calendar.add(Calendar.YEAR, -shuliang);
                    Date renwukaishishijian = Date.from(calendar.getTime().toInstant());
                    if(now.after(renwukaishishijian) && now.before(wanchengshijian)){
                        list2.get(i).setZhuangtai("进行中");
                        shenhefankuibiaoMapper.updateById(list2.get(i));
                    }
                }else{
                    System.out.println("暂无数据");
                }
            }
        }
    }
}
